package com.memory.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.memory.common.BaseContext;
import com.memory.common.R;
import com.memory.daomain.User;
import com.memory.service.UserService;
import com.memory.utils.SMSUtils;
import com.memory.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 邓哈哈
 * 2023/1/28 13:28
 * Function:
 * Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送短信验证码
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        //获取手机号
        String phone = user.getPhone();

        if (StringUtils.isEmpty(phone))
            return R.error("手机号不能为空!");

        //生成随机的4位验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();

        //调用阿里云提供的短信服务API完成发送短信
//            SMSUtils.sendMessage("瑞吉外卖", "", phone, code);
        log.info("code={}", code);

        //需要将生成的验证码保存到Session
        request.getSession().setAttribute(phone, code);

        return R.success("短信验证码发送成功!");
    }

    /**
     * 移动端用户登录
     *
     * @param map
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpServletRequest request) {
        //获取手机号
        String phone = map.get("phone").toString();

        //获取验证码
        String code = map.get("code").toString();

        //从Session中获取保存的验证码
        Object codeInSession = request.getSession().getAttribute(phone);

        //进行验证码校验
//        if (!(codeInSession != null && codeInSession.equals(code)))//如果验证码输入错误,说明登陆失败
        if (codeInSession == null )//如果验证码输入错误,说明登陆失败
            return R.error("验证码有误!登录失败!");

        //判断当前手机号对应的用户是否为新用户,如果是,新用户就自动完成注册
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        //设置判断条件
        lqw.eq(phone != null, User::getPhone, phone);
        //执行查询
        User user = userService.getOne(lqw);

        if (user == null) {//该用户为新用户,需要注册

            user = new User();//新开辟一个空间

            user.setPhone(phone);//记录电话

            user.setStatus(1);//记录状态(默认为可用:1)

            userService.save(user);//添加用户
        }
        //拿取该用户的id
        User one = userService.getOne(lqw);
        log.info("该用户d等于 = {}", one.getId());
        request.getSession().setAttribute("user", one.getId()); //将用户id保存至Session中

        return R.success(user);
    }

    /**
     * 登出外卖系统
     *
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request) {
        //清理Session中保存的当前员工的id(对应登录操作的第6步)
        request.getSession().removeAttribute("user");

        return R.success("退出成功");
    }


}
